package com.haoniu.common.service.impl;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.comm.ResponseMessage;
import com.aliyun.oss.model.PutObjectResult;
import com.haoniu.common.service.AliOssService;
import com.haoniu.common.utils.huanxin.HuanXinUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * 描述:
 * 阿里云云存储
 *
 * @author shilvfei
 * @create 2018-11-16 12:20
 */
@Service
public class AliOssServiceImpl implements AliOssService {

    @Value("${ali.file.endpoint}")
    private String endpoint;

    @Value("${ali.file.accessKey}")
    private String accessKeyId;

    @Value("${ali.file.secretKey}")
    private String accessKeySecret;

    @Value("${ali.file.bucket}")
    private String bucketName;

    @Value("${file.base.url}")
    private String fileUrl;

    @Override
    public String getFileBaseUrl() {
        return fileUrl;
    }

    @Override
    public String getToken() {
        return null;
    }

    @Override
    public String uploadFile(MultipartFile uploadFile, String key) throws Exception {
        // 创建OSSClient实例。
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        // 上传内容到指定的存储空间（bucketName）并保存为指定的文件名称（objectName）。
        ossClient.putObject(bucketName, key, uploadFile.getInputStream());
        // 关闭OSSClient。
        ossClient.shutdown();
        return key;
    }
}
